package Game;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.imageio.ImageIO;

public class Dealer {
	
	private List<Card> allCards = new ArrayList<Card>();
	private List<Card> first = new ArrayList<Card>();
	private List<Card> second = new ArrayList<Card>();
	private List<Card> third = new ArrayList<Card>();
	private List<Card> fourth = new ArrayList<Card>();
	private List<Point> points = new ArrayList<Point>();
	private Image cardImage;
	private Point pos;
	
	public Dealer(){
		init();
		sequence(first);
		initBack();
		setPos();
	}
	
	public void sequence(List<Card> list){
		for (int i = list.size(); i > 2; i--) {
			for (int j = 0; j < (i-1); j++) {
				if(list.get(j).getColor() > list.get(j+1).getColor()){
					Card card = list.get(j);
					list.set(j, list.get(j+1));
					list.set(j+1, card);
				}
			}
		}
		for (int i = list.size(); i > 2; i--) {
			for (int j = 0; j < (i-1); j++) {
				if(list.get(j).getColor() == list.get(j+1).getColor()){
					if(list.get(j).getKindOf() > list.get(j+1).getKindOf()){
						Card card = list.get(j);
						list.set(j, list.get(j+1));
						list.set(j+1, card);
					}
				}
			}
		}
	}
	
	public void initBack(){
		try {
			cardImage = ImageIO.read(new File("images\\cardback.jpg"));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	private void init(){
		for (int i = 0; i < 4; i++) {
			for (int j = 0; j < 8; j++) {
				allCards.add(new Card(i, j));
			}
		}
		Collections.shuffle(allCards);
		for (int j = 0; j < 8; j++) {
			first.add(allCards.get(j));
			second.add(allCards.get(8+j));
			third.add(allCards.get(16+j));
			fourth.add(allCards.get(24+j));
		}
	}
	
	private void setPos(){
		pos = new Point(250,500);
		for (int i = 0; i < first.size(); i++){
			first.get(i).setPos(pos.x, pos.y);
			//second.get(i).setPos(pos.x, pos.y);
			//third.get(i).setPos(pos.x, pos.y);
			//fourth.get(i).setPos(pos.x, pos.y);
			pos.x += 48;
		}
		pos = new Point(900,150);
		for (int i = 0; i < second.size(); i++) {
			//second.get(i).setPos(pos.x, pos.y);
			points.add(pos);
			pos = new Point(pos.x, pos.y+30);
		}
		pos = new Point(250,20);
		for (int i = 0; i < third.size(); i++) {
			//third.get(i).setPos(pos.x, pos.y);
			points.add(pos);
			pos = new Point(pos.x+48, pos.y);
		}
		pos = new Point(20, 150);
		for (int i = 0; i < fourth.size(); i++) {
			//fourth.get(i).setPos(pos.x, pos.y);
			points.add(pos);
			pos = new Point(pos.x, pos.y+30);
		}
	}
	
	public void mouseClicked(MouseEvent arg0) {
		for (int i = 0; i < allCards.size(); i++) {
			allCards.get(i).mouseClicked(arg0);
		}
	}
	
	public List<Card> getAllCards() {
		return allCards;
	}
	
	public void draw(Graphics g){
		for (int i = 0; i < 8; i++) {
			first.get(i).draw(g);
		}
		for (int i = 0; i < points.size(); i++) {
			g.drawImage(cardImage, points.get(i).x, points.get(i).y, null);
		}
	}

	public void update() {
		for (int i = 1; i < first.size(); i++) {
			if(first.get(i).isTaked()){
				first.get(i-1).setForTake(true);
			}
		}
	}
}